package com.liik.game.mvc.object;

import com.liik.game.util.Rectangle;

public class Menu {
		
	public static class Item {
		public final int id;
		public boolean visible;
		public Rectangle region;
		public String caption;
		
		public Item(int id, float x, float y, float width, float height, String caption) {
			this.id = id;
			this.region = new Rectangle(x, y, width, height);
			this.caption = caption;
			this.visible = true;
		}
	}
	
	private final Item[] menuItems;
	private int nextFreeSlot;
	
	public Menu(int maxItems) {
		menuItems = new Item[maxItems];
		nextFreeSlot = 0;
	}
	
	public Item getItem(float x, float y) {
		for (int i = 0; i < menuItems.length; ++i) {
			if (menuItems[i] != null && menuItems[i].region.contains(x, y)) {
				return menuItems[i];
			}
		}
		return null;
	}
	
	public Item getItem(int itemId) {
		if (itemId >= 0 && itemId < menuItems.length)
			return menuItems[itemId];
		return null;
	}
	
	public int addItem(Item item) {
		if (nextFreeSlot < menuItems.length) {
			menuItems[nextFreeSlot] = item;
			return nextFreeSlot++;
		}
		throw new RuntimeException("Too many menu items!");
	}

	public int getNumberOfItems() {
		return nextFreeSlot;
	}	
}
